﻿using System;
using System.Collections.Generic;
using System.Data;
namespace BLL.Sec
{
    /// <summary>
    /// 用户类，一个实例是用户的抽象表示
    /// </summary>
    public class User
    {

        private readonly OracleDAL.Sec.User userDAL = new OracleDAL.Sec.User();
        private readonly OracleDAL.Sec.LogonStatus logonStatusDAL = new OracleDAL.Sec.LogonStatus();
        private readonly OracleDAL.Sec.LogonLog logonLogDAL = new OracleDAL.Sec.LogonLog();

        public Model.Sec.User GetModel(string UserISN)
        {
            return userDAL.GetModel(UserISN);
        }
        public List<Model.Sec.UserGroup> UserGroups (string UserISN)
        {
            return userDAL.GetUserGroups(UserISN);
        }

        #region User的构造方法
        /// <summary>
        /// 用户类构造函数
        /// </summary>
        public User()
        {
        }
        #endregion

        #region 登录，静态方法
        /// <summary>
        /// 用户登录函数
        /// </summary>
        /// <param name="UserISN">用户ISN</param>
        /// <param name="Password">用户密码</param>
        /// <returns>登录状态</returns>
        public  Model.Sec.LogonStatus CheckLogon(string UserISN, string Password)
        {
            return CheckLogon(UserISN, Password, null,null);
        }

        /// <summary>
        /// 用户登录函数。此函数对比IP，使用默认密钥(Metarnet)加密密码后再对比密码
        /// </summary>
        /// <param name="UserISN">用户ISN</param>
        /// <param name="Password">用户密码</param>
        /// <param name="IP">用户IP</param>
        /// <returns>登录状态</returns>
        public Model.Sec.LogonStatus CheckLogon(string UserISN, string Password, string IP)
        {
            return CheckLogon(UserISN, Password, null, IP);
        }
        /// <summary>
        /// 用户登录函数。密钥为null时使用默认密钥（Metarnet）加密密码再执行对比；IP为null时不执行IP对比
        /// </summary>
        /// <param name="UserISN">用户ISN</param>
        /// <param name="Password">用户密码</param>
        /// <param name="Key">加密密钥</param>
        /// <param name="IP">用户IP</param>
        /// <returns>登录状态</returns>
        public Model.Sec.LogonStatus CheckLogon(string UserISN, string Password, string Key, string IP)
        {
            Model.Sec.LogonStatus logonStatus;
            Model.Sec.User user = userDAL.GetModel(UserISN);


            //用户是否存在
            if (user.UserName != null)
            {
                Key = Key == null ? "Metarnet" : Key;
                //密码是否正确
                if (user.Password != Crypt.Encrypt(Password, Key))
                {
                    //错误代码-2：密码不正确（代号及含义可以在字典表SEC_DICTIONARY中更改）
                    //logonStatus =  new Model.Sec.LogonStatus(-2,user);
                    logonStatus = logonStatusDAL.GetModel(-2, user);
                }
                else
                {
                    //验证多个IP
                    string[] IPs = user.IPSection.Split(',');
                    int logonFlag = -3;

                    if (user.IPSection.IndexOf("0.0.0.0") > -1)
                    {
                        logonFlag = 0;
                    }
                    else
                    {
                        foreach (string userIP in IPs)
                        {
                            string mask = userIP;

                            if (IP == mask)
                            {
                                logonFlag = 0; break;
                            }

                            //下面是进行全匹配
                            if (userIP.EndsWith(".0"))
                            {
                                mask = userIP.Replace(".0", "");
                            }
                            string cilentIP = IP;

                            string[] arrMark = mask.Split('.');
                            string[] arrIP = cilentIP.Split('.');

                            bool matchFlag = true;
                            for (int i = 0; i < arrMark.Length; i++)
                            {
                                if (arrMark[i] != arrIP[i])
                                {
                                    matchFlag = false;
                                    break;
                                }
                            }
                            if (matchFlag)
                            {
                                logonFlag = 0;
                                break;
                            }
                        }
                    }
                    logonStatus = logonStatusDAL.GetModel(logonFlag, user);

                }
            }
            else
            {
                //错误代码-1：用户不存在（代号及含义可以在字典表SEC_DICTIONARY中更改）
                logonStatus = logonStatusDAL.GetModel(-1);
            }
            return logonStatus;
        }
        #endregion


        #region 新增用户，静态方法
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="UserISN">用户ISN</param>
        /// <param name="UnitISN">所属单位ISN</param>
        /// <param name="UserName">用户名</param>
        /// <param name="Password">密码</param>
        /// <param name="UnitName">所属单位名称</param>
        /// <param name="CreateTime">创建时间爱你</param>
        /// <param name="Position">职称</param>
        /// <param name="Tel">电话</param>
        /// <param name="EMail">电子邮件地址</param>
        /// <param name="IPSection">允许登录的IP网段</param>
        /// <param name="Sort">显示顺序</param>
        /// <returns>是否成功</returns>
        public bool NewUser(string UserISN,string UnitISN,string UserName,string Password,string UnitName,DateTime CreateTime,string Position,string Tel,string EMail,string IPSection,int Sort)
        {
            return userDAL.NewUser(UserISN, UnitISN, UserName, Crypt.Encrypt(Password,null), UnitName, CreateTime, Position, Tel, EMail, IPSection, Sort);
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool NewUser(Model.Sec.User user)
        {
             return  this.NewUser(user.UserISN, user.UnitISN, user.UserName,user.Password, user.UnitName, user.CreateTime, user.Position, user.Tel, user.EMail, user.IPSection, user.Sort);
        }
        #endregion

        #region 删除用户，静态方法和实例方法
        /// <summary>
        /// 删除用户,用户组与用户的对应关系,用户与节点的关系
        /// </summary>
        /// <param name="UserISN">用户ISN</param>
        /// <returns>删除是否成功</returns>
        public bool DeleteUser(string UserISN)
        {
            return userDAL.DeleteUser(UserISN);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="user">用户对象实例</param>
        /// <returns>删除是否成功</returns>
        public bool DeleteUser(Model.Sec.User user)
        {
            return userDAL.DeleteUser(user.UserISN);
        }
        #endregion

        #region 更新用户资料，实例方法
        /// <summary>
        /// 更新用户资料
        /// </summary>
        /// <returns>更新是否成功</returns>
        public bool Update(Model.Sec.User user)
        {
            return userDAL.UpdateUser(user);
        }
        #endregion

        #region 用户在线状态，实例方法
        /// <summary>
        /// 进行用户在线状态的更新
        /// </summary>
        /// <returns>更新是否成功</returns>
        public bool UpdateMyStatus(string UserISN)
        {
            return userDAL.UpdateMyStatus(UserISN);
        }
        /// <summary>
        /// 根据用户的在线时间，在两个更新周期内更新过的用户认为在线。
        /// </summary>
        /// <param name="UnitISN">单位ISN</param>
        /// <returns>在线用户的集合</returns>
        public Model.Sec.UserCollection GetOnlineUserList(string UnitISN)
        {
            return userDAL.GetOnlineUsers(UnitISN);
        }
        #endregion
        /// <summary>
        /// 判断是否存在记录
        /// </summary>
        /// <param name="StrWhere"></param>
        /// <returns></returns>
        public bool existUser(string StrWhere)
        {
            return userDAL.existUser(StrWhere);
        }
        /// <summary>
        /// 判断用户是否是根用户
        /// </summary>
        /// <param name="userISN"></param>
        /// <returns></returns>
        public bool isRootsGroupUser(string userISN)
        {
            return userDAL.isRootsGroupUser(userISN);

        }
        public int getMaxSort(string strWhere)
        {
            return userDAL.getMaxSort(strWhere);
        }
            /// <summary>
        /// 根据上级的NodeID从单位表中查找用户
        /// </summary>
        /// <param name="NodeID"></param>
        /// <returns></returns>
        public Model.Sec.User[] getArrUsers(string NodeID)
        {
            return userDAL.getArrUsers(NodeID);
        }
        /// <summary>
        /// 根据上级的NodeID从单位表中查找用户
        /// </summary>
        /// <param name="NodeID"></param>
        /// <param name="isRoot">是否是管理员root</param>
        /// <returns></returns>
        public DataTable getDataTable(string NodeID,bool isRoot)
        {
            return userDAL.getDataTable(NodeID,isRoot);
        }
       /// <summary>
       /// 从单位表中查找用户
       /// </summary>
       /// <param name="unitisn"></param>
        /// <param name="isRoot">是否是管理员root</param>
       /// <returns></returns>
        public DataTable getUserDataTable(string unitisn,bool isRoot)
        {
            return userDAL.getUserDataTable(unitisn,isRoot);
        }
           /// <summary>
        /// 返回用户的权限，权限以,号分开
        /// </summary>
        /// <param name="userisn">用户isn</param>
        /// <returns></returns>
        public string getOperateID(string userisn)
        {
            return userDAL.getOperateID(userisn);
        }
         /// <summary>
        /// 返回用户在页面的权限OperateID，common
        /// </summary>
        /// <param name="UserISN"></param>
        /// <param name="ParentOperateID"></param>
        /// <returns></returns>
        public DataTable getChildOperateID(string UserISN, string ParentOperateID)
        {
            return userDAL.getChildOperateID(UserISN, ParentOperateID);
        }
         /// <summary>
        /// 返回下一级有权限的OPerateID，operatename
        /// </summary>
        /// <param name="UserISN"></param>
        /// <param name="ParentOperateID"></param>
        /// <returns></returns>
        public DataTable getTableChildOperateID(string UserISN, string ParentOperateID)
        {
            return userDAL.getTableChildOperateID(UserISN, ParentOperateID);
        }
         /// <summary>
        /// 返回下一级所有有权限的OPerateID，operatename
        /// </summary>
        /// <param name="UserISN"></param>
        /// <param name="ParentOperateID"></param>
        /// <returns></returns>
        public DataTable getAllChildOperateID(string UserISN, string ParentOperateID)
        {
            return userDAL.getAllChildOperateID(UserISN, ParentOperateID);
        }
         /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="PassWord">原文</param>
        /// <param name="UserISN">用户ISN</param>
        /// <returns></returns>
        public bool ChangePwd(string PassWord, string UserISN)
        {
            return userDAL.ChangePwd(Crypt.Encrypt(PassWord, null), UserISN);
        }
     
    }
}
